/*
@title: Monkey quest 3: lightless knights
@description: "Monkey Quest 3: Lightless Knights" is an action-adventure game where you play as a brave monkey. Your objective is to protect your banana from the encroaching lightless knights by stopping them before they reach it. Maneuver around the map to fend off these foes and defend your precious banana.
@author: Eamonn
@tags: []
@addedOn: 2024-08-20
First time? Check out the tutorial game:
https://sprig.hackclub.com/gallery/getting_started

*/

let r = 0;
let qy = 0;
let qx = 6;
let wy = 0;
let wx = 0;
let x = 0;
let speed = 1000;
let score = 0;

/*
   const fireBeats = tune`
250: G5~250,
250: F5~250,
250: G4~250,
250: B4~250,
250: G4~250,
250,
250: G4~250,
250: E4~250,
250: E5~250,
250: G5~250,
250: E5~250,
250: C5~250,
250: E5~250,
250: G5~250,
250: E5~250,
250: C5~250,
250: G4~250,
250: B4~250,
250: G4~250,
250: E4~250,
250: G4~250,
250: B4~250,
250: G4~250,
250: E4~250,
250: E5~250,
250: G5~250,
250: E5~250,
250: C5~250,
250: E5~250,
250: G5~250,
250: E5~250,
250: C5~250`



   const fireBeats2 = tune`
138.24884792626727: F5~138.24884792626727,
138.24884792626727: E5~138.24884792626727,
138.24884792626727: G4~138.24884792626727,
138.24884792626727: B4~138.24884792626727 + E5^138.24884792626727,
138.24884792626727: G4~138.24884792626727 + A5-138.24884792626727,
138.24884792626727: E5^138.24884792626727,
138.24884792626727: G4~138.24884792626727 + F5^138.24884792626727,
138.24884792626727: E4~138.24884792626727,
138.24884792626727: E5~138.24884792626727 + F5^138.24884792626727 + B5-138.24884792626727,
138.24884792626727: G5~138.24884792626727,
138.24884792626727: E5~138.24884792626727,
138.24884792626727: C5~138.24884792626727 + B4^138.24884792626727,
138.24884792626727: E5~138.24884792626727,
138.24884792626727: G5~138.24884792626727 + G4^138.24884792626727,
138.24884792626727: E5~138.24884792626727 + G4^138.24884792626727 + G5-138.24884792626727,
138.24884792626727: C5~138.24884792626727,
138.24884792626727: G4~138.24884792626727 + E4^138.24884792626727,
138.24884792626727: B4~138.24884792626727 + E4^138.24884792626727 + D4^138.24884792626727,
138.24884792626727: G4~138.24884792626727,
138.24884792626727: E4~138.24884792626727 + D5^138.24884792626727 + A5-138.24884792626727,
138.24884792626727: G4~138.24884792626727,
138.24884792626727: B4~138.24884792626727,
138.24884792626727: G4~138.24884792626727 + A5-138.24884792626727,
138.24884792626727: E4~138.24884792626727,
138.24884792626727: E5~138.24884792626727,
138.24884792626727: G5~138.24884792626727 + B4^138.24884792626727,
138.24884792626727: E5~138.24884792626727 + B5-138.24884792626727,
138.24884792626727: C5~138.24884792626727,
138.24884792626727: E5~138.24884792626727 + F4^138.24884792626727 + E4^138.24884792626727,
138.24884792626727: G5~138.24884792626727 + A4^138.24884792626727,
138.24884792626727: E5~138.24884792626727,
138.24884792626727: C5~138.24884792626727`

   const fireBeats3 = tune`
159.5744680851064: D4^159.5744680851064,
159.5744680851064: E4^159.5744680851064,
159.5744680851064: D4^159.5744680851064,
159.5744680851064: E4^159.5744680851064,
159.5744680851064: D4^159.5744680851064,
159.5744680851064: E4^159.5744680851064,
159.5744680851064: D4^159.5744680851064,
159.5744680851064: E4^159.5744680851064,
159.5744680851064: D4^159.5744680851064,
159.5744680851064: E4^159.5744680851064 + B4~159.5744680851064,
159.5744680851064: C5~159.5744680851064,
159.5744680851064: B4~159.5744680851064,
159.5744680851064: C5~159.5744680851064,
159.5744680851064: B4~159.5744680851064,
159.5744680851064: C5~159.5744680851064,
159.5744680851064: B4~159.5744680851064 + E4^159.5744680851064,
159.5744680851064: D4^159.5744680851064,
159.5744680851064: E4^159.5744680851064,
159.5744680851064: D4^159.5744680851064,
159.5744680851064: E4^159.5744680851064,
159.5744680851064: D4^159.5744680851064,
159.5744680851064: E4^159.5744680851064,
159.5744680851064: D4^159.5744680851064 + B4~159.5744680851064,
159.5744680851064: C5~159.5744680851064,
159.5744680851064: B4~159.5744680851064,
159.5744680851064: C5~159.5744680851064,
159.5744680851064: B4~159.5744680851064,
159.5744680851064: C5~159.5744680851064,
159.5744680851064: B4~159.5744680851064,
159.5744680851064: C5~159.5744680851064,
159.5744680851064: D4^159.5744680851064,
159.5744680851064: E4^159.5744680851064`
 const fireBeats4 = tune`
177.5147928994083: F4-177.5147928994083 + G4-177.5147928994083,
177.5147928994083: C4/177.5147928994083 + G4-177.5147928994083,
177.5147928994083: C4/177.5147928994083 + D4/177.5147928994083 + E4^177.5147928994083 + G4-177.5147928994083,
177.5147928994083: C4~177.5147928994083 + D4/177.5147928994083 + E4/177.5147928994083 + F4^177.5147928994083 + G4-177.5147928994083,
177.5147928994083: D4~177.5147928994083 + E4/177.5147928994083 + G4^177.5147928994083,
177.5147928994083: E4/177.5147928994083 + F4/177.5147928994083 + A4^177.5147928994083,
177.5147928994083: F4/177.5147928994083 + G4/177.5147928994083 + A4^177.5147928994083 + B4^177.5147928994083,
177.5147928994083: G4/177.5147928994083 + A4/177.5147928994083 + B4^177.5147928994083,
177.5147928994083: A4/177.5147928994083 + B4^177.5147928994083 + C5^177.5147928994083,
177.5147928994083: B4/177.5147928994083 + C5^177.5147928994083 + D5^177.5147928994083 + E5^177.5147928994083,
177.5147928994083: D5/177.5147928994083 + E5^177.5147928994083 + F5^177.5147928994083 + E4~177.5147928994083 + F4~177.5147928994083,
177.5147928994083: F5^177.5147928994083 + G5^177.5147928994083 + A4~177.5147928994083 + C4/177.5147928994083,
177.5147928994083: G5^177.5147928994083 + F5^177.5147928994083 + A4~177.5147928994083 + G4~177.5147928994083 + C4/177.5147928994083,
177.5147928994083: F5^177.5147928994083 + G4~177.5147928994083 + C4/177.5147928994083,
177.5147928994083: E5^177.5147928994083 + D5^177.5147928994083 + D4-177.5147928994083,
177.5147928994083: D5^177.5147928994083 + C5^177.5147928994083 + D4-177.5147928994083 + E4-177.5147928994083 + A4~177.5147928994083,
177.5147928994083: C5^177.5147928994083 + B4^177.5147928994083 + E4-177.5147928994083 + F4-177.5147928994083 + A4~177.5147928994083,
177.5147928994083: A4^177.5147928994083 + F4-177.5147928994083 + E4-177.5147928994083,
177.5147928994083: A4^177.5147928994083 + E4-177.5147928994083,
177.5147928994083: B4^177.5147928994083 + E4-177.5147928994083 + F4-177.5147928994083,
177.5147928994083: B4^177.5147928994083 + F4-177.5147928994083 + G4-177.5147928994083,
177.5147928994083: B4-177.5147928994083 + C5^177.5147928994083 + A4-177.5147928994083,
177.5147928994083: C5-177.5147928994083 + B4-177.5147928994083,
177.5147928994083: D5^177.5147928994083 + E5^177.5147928994083,
177.5147928994083: E5^177.5147928994083 + F5^177.5147928994083 + B4/177.5147928994083 + C5/177.5147928994083 + G5^177.5147928994083,
177.5147928994083: F5^177.5147928994083 + E5^177.5147928994083 + B4/177.5147928994083 + G5^177.5147928994083 + B5^177.5147928994083,
177.5147928994083: E5^177.5147928994083 + D5^177.5147928994083 + F4/177.5147928994083 + G4/177.5147928994083 + A4/177.5147928994083,
177.5147928994083: D5^177.5147928994083 + C5^177.5147928994083 + B4^177.5147928994083 + E4/177.5147928994083 + F4/177.5147928994083,
177.5147928994083: B4^177.5147928994083 + E4/177.5147928994083 + A4/177.5147928994083 + G4/177.5147928994083 + E5^177.5147928994083,
177.5147928994083: B4^177.5147928994083 + C5^177.5147928994083 + D4/177.5147928994083 + E4/177.5147928994083 + G4/177.5147928994083,
177.5147928994083: C5^177.5147928994083 + E5^177.5147928994083 + C4/177.5147928994083 + D4/177.5147928994083 + F4/177.5147928994083,
177.5147928994083: D5^177.5147928994083 + C5^177.5147928994083 + B4^177.5147928994083 + E4/177.5147928994083`

//const playback = playTune(fireBeats, Infinity)
//playback.end()
//const playback2 = playTune(fireBeats2, Infinity)
//const playback3 = playTune(fireBeats3, Infinity)
//const playback4 = playTune(fireBeats4, Infinity)

*/

function updateScore() {
  // Assuming addText is a function that displays text on the screen
  addText(`score: ${score}`, { 
    x: 6,
    y: 2,
    color: color`3`
  });

}

 function end() {
   addText("game over", { 
 x: 6,
  y: 3,
color: color`9`})
//playback.end()
     
    getFirst(banana).remove();
  

    getFirst(lightless).remove();
  


    getFirst(lightless2).remove();
// playback.end()
}
/*

function checkForSprite() {
  const rocketc = getAll(rocket1);
}
*/
function runOncePerSecond() {

  const lightlessObjects = getAll(lightless);

 
  lightlessObjects.forEach((lightless) => {
  
    if (lightless.x >= 4) {
     
      lightless.x -= 1;
    } else if(lightless.x <= 2) {  lightless.x += 1; }
  
    if (lightless.y <= 3) {
    
      lightless.y += 1;
    } else if(lightless.y >=3) {  lightless.y -= 1; }
  });
//-----------------------------
  
  const lightlessObjects2 = getAll(lightless2);

 
  lightlessObjects2.forEach((lightless2) => {
  
    if (lightless2.x >= 4) {
     
      lightless2.x -= 1;
    } else if(lightless2.x <= 2) {  lightless2.x += 1; }
  
    if (lightless2.y <= 3) {
    
      lightless2.y += 1;
    } else if(lightless2.y >=3) {  lightless2.y -= 1; }
  });


}
speed = (1000 - score*10)+100

 
function  runOnceforthPerSecond (){
/*
if (score < 10){playback2.end()
  
  const playback = playTune(fireBeats, Infinity)
} else if (score > 10){ playback.end()
  const playback = playTune(fireBeats2, Infinity)
}else if(score > 25) {
  playback.end()
  const playback = playTune(fireBeats3, Infinity)
}
*/
  
    const error = tilesWith(lightless2, lightless);
  if (error.length > 0) {    console.log("error.patch");
  getFirst(lightless).remove();
 addSprite(qx,qy,lightless);}

   const blocks = tilesWith(monkey, lightless);
  if (blocks.length > 0) { score +=1,
  getFirst(lightless).remove();
 addSprite(qx,qy,lightless);
                         }

  const blocks2 = tilesWith(monkey, lightless2);
  if (blocks2.length > 0) { score +=1,
  getFirst(lightless2).remove();
 addSprite(qx,qy,lightless2);
                         }
  
  const lose = tilesWith(banana, lightless);
 if (lose.length > 0) {end()}

    const lose2 = tilesWith(banana, lightless2);
 if (lose2.length > 0) { end() }
                     
//const lose = tilesWith(banana,lightless);
  /*
if (lose.length > 0){
   addText("game over", { 
    x: 6,
    y: 2,
    color: color`5`
  });
  console.log("Memes");}
                      */    
  updateScore();
 

   r = Math.floor(Math.random() * 23);
if (r == 0){
  qx = 0;
  qy = 0;
}else if (r == 1){
    qx = 1;
  qy = 0;
}else if (r == 2){
    qx = 2;
  qy = 0;
}else if (r == 3){
    qx = 3;
  qy = 0;
}else if (r == 4){
    qx = 4;
  qy = 0;
}else if (r == 5){
    qx = 5;
  qy = 0;
}else if (r == 6){
    qx = 6;
  qy = 0;
}else if (r == 7){
    qx = 6;
  qy = 1;
}else if (r == 8){
    qx = 6;
  qy = 2;
}else if (r == 9){
    qx = 6;
  qy = 3;
}else if (r == 10){
    qx = 6;
  qy = 4;
}else if (r == 11){
    qx = 6;
  qy = 5;
}else if (r == 12){
    qx = 6;
  qy = 6;
}else if (r == 13){
    qx = 5;
  qy = 6;
}else if (r == 14){
    qx = 4;
  qy = 6;
}else if (r == 15){
    qx = 3;
  qy = 6;
}else if (r == 16){
    qx = 2;
  qy = 6;
}else if (r == 17){
    qx = 1;
  qy = 6;
}else if (r == 18){
    qx = 0;
  qy = 6;
}else if (r == 19){
    qx = 0;
  qy = 5;
}else if (r == 20){
    qx = 0;
  qy = 4;
}else if (r == 21){
    qx = 0;
  qy = 3;
}else if (r == 22){
    qx = 0;
  qy = 2;
}else if (r == 23){
    qx = 0;
  qy = 1;
}


  }
  //  console.log("Memes",r,qx,qy,speed);

const monkey = "m"
const banana = "b"
const lightless = "l"
const rocket1 = "1"
const lightless2 = "s"
const floor = "f"
setLegend(
  [ monkey, bitmap`
................
................
...........CCC..
..........C0C0C.
..........CCCCC.
.CCCCCCCCCCCCCC.
.CCCCCCCCCCCCC..
.CCCCCCCCCCC....
.CCCCCCCCCCC....
.CCCCCCCCCCC....
.CCCCCCCCCCC....
.CCCCCCCCCCC....
.CCCCCCCCCCC....
.CCCCCCCCCCC....
.CCCCCCCCCCC....
.CCCCCCCCCCC....` ],
    [ banana, bitmap`
................
.......660......
......6666......
.....66666......
....66666.......
....6666........
....6666........
....6666........
....6666........
....6666........
....66666.......
.....6666.......
......6666......
......66666.....
......66666.....
........666.....` ],
   [ lightless, bitmap`
................
......LLL.......
......L33.......
......LLL.......
....LLLLLL......
...LLLLLLLL.....
...L.LLLL.L.....
..LL.LLLL.LL....
.....LLLL.......
.....LLLL.......
.....LLLL.......
.....L..L.......
.....L..L.......
.....L..L.......
.....L..L.......
.....LL.LL......` ],
   [ rocket1, bitmap`
................
................
................
................
................
.............L..
.999111111111LL.
333311F1F1F11LLL
.999111111111LL.
.............L..
................
................
................
................
................
................` ],
    [ lightless2, bitmap`
................
......LLL.......
......L33.......
......LLL.......
....LLLLLL......
...LLLLLLLL.....
...L.LLLL.L.....
..LL.LLLL.LL....
.....LLLL.......
.....LLLL.......
.....LLLL.......
.....L..L.......
.....L..L.......
.....L..L.......
.....L..L.......
.....LL.LL......` ],
      [ floor, bitmap`
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111` ],
)



setSolids([monkey,banana])

let level = 0
const levels = [
  map`
...s..l
.......
...m...
...b...
.......
.......
.......`
]

setMap(levels[level])
/*
setPushables({
  [  ]: []
})
*/


onInput("j", () => {
 setMap(levels[level]);
  clearText()
score = 0;
    addText(`score: ${score}`, { 
    x: 6,
    y: 2,
    color: color`3` });
     r = Math.floor(Math.random() * 23);
})
onInput("s", () => {
  getFirst(monkey).y += 1
})


onInput("w", () => {
  getFirst(monkey).y -= 1
})


onInput("a", () => {
  getFirst(monkey).x -= 1
})


onInput("d", () => {
  getFirst(monkey).x += 1
})



//if (blocks.length > 0){wx = getFirst(monkey).x,wy = getFirst(monkey).y, clearTile(wx,wy), addsprite(wx,wy,monkey)  }
/*

afterInput(() => {
//  if(getFirst(rocket1).y = 10){sprite.remove()}

})
checkForSprite();


function runOnceforthPerSecond() {  
 getFirst(rocket1).y += 1
  if(x > 0 ){
  
  
   if(getFirst(rocket1).y = 5){getFirst(rocket1).remove(), x -=1}
  }
}


onInput("l", () => {

let rocket = addSprite(getFirst(monkey).x, getFirst(monkey).y, rocket1, x +=1);
  
  }
  
);
*/

setBackground("f")
const intervalId = setInterval(runOnceforthPerSecond, 25);
 const  intervalId1 = setInterval(runOncePerSecond, 1100 );
